package com.Image.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import javax.imageio.ImageIO;

public class getThumbImage {
	private static String DEFAULT_PREVFIX = "thumb_";
	 private static Boolean DEFAULT_FORCE = false;
	    
	 public static File   thumbnailImage(File imgFile, int w, int h, String prevfix, boolean force){
	        if(imgFile.exists()){
	            try {
	                // ImageIO 支持的图片类型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
	                String types = Arrays.toString(ImageIO.getReaderFormatNames());
	                String suffix = null;
	                // 获取图片后缀
	                if(imgFile.getName().indexOf(".") > -1) {
	                    suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
	                }// 类型和图片后缀全部小写，然后判断后缀是否合法
	                if(suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0){
	                    System.out.println("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);
	                    return null;
	                }
	                Image img = ImageIO.read(imgFile);
	                if(!force){
	                    // 根据原图与要求的缩略图比例，找到最合适的缩略图比例
	                    int width = img.getWidth(null);
	                    int height = img.getHeight(null);
	                    if((width*1.0)/w < (height*1.0)/h){
	                        if(width > w){
	                            h = Integer.parseInt(new java.text.DecimalFormat("0").format(height * w/(width*1.0)));
	                           
	                        }
	                    } else {
	                        if(height > h){
	                            w = Integer.parseInt(new java.text.DecimalFormat("0").format(width * h/(height*1.0)));
	                           
	                        }
	                    }
	                }
	                BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
	                
	                Graphics2D g = bi.createGraphics();
	                // 设置“抗锯齿”的属性  
	                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
	                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
	                g.drawImage(img.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, null);  
	                //Graphics g = bi.getGraphics();
	                //g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
	                g.dispose();
	                String p = imgFile.getPath();
	                // 将图片保存在原目录并加上前缀
	                File file = new File(p.substring(0,p.lastIndexOf(File.separator)) + File.separator +prevfix+imgFile.getName());
	                ImageIO.write(bi, suffix, file);
	                return file;
	            } catch (IOException e) {
	               e.printStackTrace();
	            }
	        }else{
	           System.out.println("图片不存在");
	        }
	        return null;
	    }
	    
	  
	    
}
